Intro |
| Bla |
Scripts |
|
Calculating previous date for use in file naming
|
I wrote this script to pack the audit logs on my machine on daily basis.
#!/bin/ksh
#************************************************************************
# NAME bkupalog.sh - Script used to shut idle mml session
# COPYLEFT Alkr 2003
# All rights reserved.
#
# DESCRIPTION
# Will pack all the hourly audit log files for the previous day into /home2/Audit_log/*.tar.Z
#
# INSTALLATION
# su -
# mkdir /home2/Audit_log
# chown apioadm:apio /home2/Audit_log
# Place the script into /home,
# chmod 775 /home/bkupalog.sh
# chown root:sys /home/bkupalog.sh
# su - apioadm
# Test:
# /home/bkupalog.sh
# ls -la /home2/Audit_log
# Crontab (apioadm):
# crontab -e
# Add the following entry:
# 20 2 * * * /home/bkupalog.sh
#
# PROGRAM NAME bkupalog
#
# AUTHOR
# 2003-05-08 by Alkr
#
# REVISION
# CHANGES
# REV NO DATE NAME DESCRIPTION
# A 030625 ALKR 1st version
# B 030703 ALKR fixed bug for days < 10
#
#************************************************************************
YESTERDAY=$((`date +%d` -1))
MONTH=$((`date +%m` +0))
YEAR=`date +%Y`
if [ $YESTERDAY -eq 0 ]
then
MONTH=$((MONTH-1))
if [ $MONTH -eq 0 ]
then
MONTH=12
YEAR=$((YEAR-1))
fi
set `cal $MONTH $YEAR`
shift $(($# - 1))
YESTERDAY=$1
fi
case "$YESTERDAY" in
1|2|3|4|5|6|7|8|9)
YESTERDAY="0${YESTERDAY}"
;;
esac
case "$MONTH" in
1|2|3|4|5|6|7|8|9)
MONTH="0${MONTH}"
;;
esac
# uncomment next line for debugging
#echo $YESTERDAY $MONTH $YEAR
AFILE='/home2/Audit_log/alog-'$YEAR-$MONTH-$YESTERDAY'.tar'
# uncomment next line for debugging
#echo $AFILE
tar cvf ${AFILE} /AP/ACS/logs/ALOG/logfile/Logfile-${YEAR}-${MONTH}-${YESTERDAY}* 1>/dev/null
# uncomment next line for debugging
#tar cvf ${AFILE} /AP/ACS/logs/ALOG/logfile/Logfile-${YEAR}-${MONTH}-${YESTERDAY}*
tar tf $AFILE 1>/dev/null && echo "$AFILE: verified">/dev/null || echo "$AFILE: errors in audit log archive"
compress -fF $AFILE
if [ $? -ne 0 ]
then
echo "$AFILE: errors creating audit log file" 1>&2
exit 1
fi
exit 0
|
Killing the processes that are idle more than a specified time
|
I wrote this script to kill the processes identified by the strings in ADlist that are idle for more
than specified time.
#!/bin/sh
## Idle time limit = 1 minute
MAXTIME=5
## Space delimited terminal list
ADlist='AD-666 AD-777'
for AD in $ADlist
do
# echo "Checking: $AD"
PROC=`ps -ef | grep -i $AD|grep -v grep | awk '{print $2}' `;
if [ "$PROC" = "" ]
then
# echo "process not found, nothing to check."
continue
fi
# process exists, check for uptime
IDLETIME=`/usr/ucb/w | grep -i $AD| grep -v grep | cut -c 31-37`
if [ "$IDLETIME" = " " ]
then
#echo "Uptime is less than 1 min, Ok."
continue
fi
# uptime is more than 1 min
if [ $IDLETIME -ge $MAXTIME ]
then
# echo " Killing: $AD\t, $IDLETIME uptime, \t pid=$PROC"
kill -9 $PROC
fi
done
exit 0
|
Sending emails with attachments |
|
This is the fragment of the larger script that mails the log with log name based on date in the subject.
The output of the error condition is appended to subject line as well - the one from ${ERRORFILE} If u want to append some more information to the Subject make sure theres only one newline there. Use echo -n option to supress unwanted newlines.
ERRORFILE=${LOGFILE}.err
# Mail settings
UUPROG='/usr/bin/uuencode'
MAILPROG='/usr/bin/mail'
MAILTO='[email protected],[email protected]'
SUBJECT='Subject: Hi there! '${YEAR}-${MONTH}-${YESTERDAY};
...
...
if [ -f ${LOGFILE}.tar.Z ]
then
(echo -n ${SUBJECT}': ';head -1 ${ERRORFILE};cat ${ERRORFILE}; ${UUPROG} ${LOGFILE}.tar.Z ${LOGFILE}.tar.Z) | ${MAILPROG} ${MAILTO}
else
echo ${SUBJECT}': Cant mail '${LOGFILE}' file - file not found' | ${MAILPROG} ${MAILTO}
fi
|